package com.monitorRoutine.monitorRoutineController.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.monitorRoutine.monitorRoutineController.Initialize;
import com.monitorRoutine.monitorRoutineController.entity.ControlCommand;
import com.monitorRoutine.monitorRoutineController.enums.CommandEnums;
import com.monitorRoutine.monitorRoutineController.mapper.ControlCommandMapper;
import com.monitorRoutine.monitorRoutineController.service.ControlCommandService;
import com.monitorRoutine.monitorRoutineController.service.ExecuteCommandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

@Slf4j
@Service
public class ControlCommandServiceImpl extends ServiceImpl<ControlCommandMapper, ControlCommand> implements ControlCommandService {
    @Autowired
    ExecuteCommandService executeCommandService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    HashMap<String, Process> processHashMap = new HashMap<>();

    @Override
    public void scanCommand() {
        String NAME = Initialize.configurationMapper.get("monitorRoutineController.format.name");
        String savePath = redisTemplate.opsForList().leftPop(NAME);

        if (savePath != null) {
            ControlCommand controlCommand = JSON.parseObject(savePath, ControlCommand.class);
            try {
                controlCommand.setExecute("2");
                updateById(controlCommand);
                if (CommandEnums.REBOOT.getValue().equals(controlCommand.getCommand())) {
                    executeCommandService.rebootComputer();
                }

                if (CommandEnums.START_PYTHON.getValue().equals(controlCommand.getCommand())) {
                    Process process = executeCommandService.startPython(controlCommand.getCommandDetails());
                    processHashMap.put(controlCommand.getCommandDetails(), process);
                }

                if (CommandEnums.START_BAT.getValue().equals(controlCommand.getCommand())) {
                    executeCommandService.startBat(controlCommand.getCommandDetails());
                }
                if (CommandEnums.CLOSURE_PYTHON.getValue().equals(controlCommand.getCommand())) {
                    Process process = processHashMap.get(controlCommand.getCommandDetails());
                    executeCommandService.closurePython(process);
                }
                if (CommandEnums.TRANSFERRING_FILES.getValue().equals(controlCommand.getCommand())) {

                    executeCommandService.transferringFiles(controlCommand);
                    return;
                }
                if (CommandEnums.GIT_PULL.getValue().equals(controlCommand.getCommand())) {

                    executeCommandService.gitPull(controlCommand);
                }
                if (CommandEnums.PROCESS_EXISTS.getValue().equals(controlCommand.getCommand())) {

                    executeCommandService.processExists(controlCommand.getCommandDetails());
                }
                controlCommand.setCompletiontime(new Date());
                controlCommand.setExecute("3");
                updateById(controlCommand);
            } catch (Exception e) {
                e.printStackTrace();
                controlCommand.setCompletiontime(new Date());
                controlCommand.setExecute("4");
                updateById(controlCommand);
            }

        }

    }

    @Override
    public boolean update(ControlCommand controlCommand) {
        return updateById(controlCommand);
    }
}
